package com.carkeeper.blog.service.impl;

import com.carkeeper.blog.dao.CategoryMapper;
import com.carkeeper.blog.pojo.Category;
import com.carkeeper.blog.service.CategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @program: carkeeper_parent
 * @Date: 2020/11/25 14:47
 * @Author: qgg
 * @Description:
 */

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;


    //添加分类
    @Override
    public void add(Category category) {
        categoryMapper.insertSelective(category);
    }

    //删除分类
    @Override
    public void delete(String id) {
        categoryMapper.deleteByPrimaryKey(id);
    }

    //修改分类
    @Override
    public void update(Category category) {
        //当前修改操作的时间
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
        String updateTime = format.format(date);

        category.setUpdateTime(updateTime);
        categoryMapper.updateByPrimaryKey(category);
    }

    //查询所有
    @Override
    public List<Category> findAll() {

        List<Category> categoryList = categoryMapper.selectAll();

        return categoryList;
    }

    //通过id查询内容
    @Override
    public Category findById(String id) {
        Category category = categoryMapper.selectByPrimaryKey(id);

        return category;
    }

    //分页查询

    @Override
    public PageInfo findPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum , pageSize);
        List<Category> categoryList = categoryMapper.selectAll();
        PageInfo<Category> pageInfo = new PageInfo(categoryList);
        return pageInfo;
    }
}
